package com.whpost.service.tools;

import com.alibaba.fastjson.JSONObject;
import org.springframework.stereotype.Service;

import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import java.io.*;
import java.net.URL;
import java.security.SecureRandom;

@Service
public class WxTools {
	public JSONObject httpRequest(String requestUrl, String requestMethod, String outputStr)
	{
		JSONObject jsonObject = null;
		StringBuffer buffer = new StringBuffer();
		try {
			TrustManager[] tm = 
			{ 
					new MyXTrustManager() 
			};
			SSLContext sslContext = SSLContext.getInstance("SSL", "SunJSSE");
			sslContext.init(null, tm, new SecureRandom());

			SSLSocketFactory ssf = sslContext.getSocketFactory();

			URL url = new URL(requestUrl);
			HttpsURLConnection httpUrlConn = (HttpsURLConnection) url.openConnection();
			httpUrlConn.setSSLSocketFactory(ssf);

			httpUrlConn.setDoOutput(true);
			httpUrlConn.setDoInput(true);
			httpUrlConn.setUseCaches(false);

			httpUrlConn.setRequestMethod(requestMethod);

			if ("GET".equalsIgnoreCase(requestMethod)) {
				httpUrlConn.connect();
			}

			if (outputStr != null) {
				OutputStream outputStream = httpUrlConn.getOutputStream();
				outputStream.write(outputStr.getBytes("UTF-8"));
				outputStream.close();
			}

			InputStream inputStream = httpUrlConn.getInputStream();
			InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8");
			BufferedReader bufferedReader = new BufferedReader(inputStreamReader);

			String str = null;
			while ((str = bufferedReader.readLine()) != null) {
				buffer.append(str);
			}
			jsonObject = JSONObject.parseObject(buffer.toString());
			bufferedReader.close();
			inputStreamReader.close();

			inputStream.close();
			inputStream = null;
			httpUrlConn.disconnect();
		} 
		catch (Exception e)
		{
			ToolBean.info("httpRequest发生错误："+e.toString());
		}
		return jsonObject;
	}
	/*GET*/
	public String httpRequestget(String requestUrl, String requestMethod,String lj) 
	{
		String fhz="";
		StringBuffer buffer = new StringBuffer();
		try {
			TrustManager[] tm = 
			{ 
					new MyXTrustManager() 
			};
			SSLContext sslContext = SSLContext.getInstance("SSL", "SunJSSE");
			sslContext.init(null, tm, new SecureRandom());

			SSLSocketFactory ssf = sslContext.getSocketFactory();

			URL url = new URL(requestUrl);
			HttpsURLConnection httpUrlConn = (HttpsURLConnection) url.openConnection();
			httpUrlConn.setSSLSocketFactory(ssf);

			httpUrlConn.setDoOutput(true);
			httpUrlConn.setDoInput(true);
			httpUrlConn.setUseCaches(false);

			httpUrlConn.setRequestMethod(requestMethod);

			if ("GET".equalsIgnoreCase(requestMethod)) {
				httpUrlConn.connect();
			}

			InputStream inputStream = httpUrlConn.getInputStream();

			InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8");
			BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
			
			byte[] data = new byte[1024];
			FileOutputStream fileOutputStream = null;
			int len = 0;
			fileOutputStream = new FileOutputStream(lj);
		      while ((len = inputStream.read(data)) != -1) {
		        fileOutputStream.write(data, 0, len);
		      }
		      fileOutputStream.close();
			bufferedReader.close();
			inputStreamReader.close();

			inputStream.close();
			inputStream = null;
			httpUrlConn.disconnect();
		} 
		catch (Exception e)
		{
			ToolBean.info("httpRequest发生错误："+e.toString());
		}
		return fhz;
	}
	public String httpRequestxml(String requestUrl, String requestMethod, String outputStr) 
	{
		String rtn="";
		try{
			StringBuffer buffer = new StringBuffer();
			URL url = new URL(requestUrl);
			HttpsURLConnection httpUrlConn = (HttpsURLConnection) url.openConnection();

			httpUrlConn.setDoOutput(true);
			httpUrlConn.setDoInput(true);
			httpUrlConn.setUseCaches(false);

			httpUrlConn.setRequestMethod(requestMethod);

			if ("GET".equalsIgnoreCase(requestMethod)) {
				httpUrlConn.connect();
			}

			if (outputStr != null) {
				OutputStream outputStream = httpUrlConn.getOutputStream();
				outputStream.write(outputStr.getBytes("UTF-8"));
				outputStream.close();
			}

			InputStream inputStream = httpUrlConn.getInputStream();
			InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8");
			BufferedReader bufferedReader = new BufferedReader(inputStreamReader);

			String str = null;
			while ((str = bufferedReader.readLine()) != null) {
				buffer.append(str);
			}
			rtn = buffer.toString();
			bufferedReader.close();
			inputStreamReader.close();

			inputStream.close();
			inputStream = null;
			httpUrlConn.disconnect();
			return rtn;
		} 
		catch (Exception e)
		{
			ToolBean.info("httpRequest发生错误："+e.toString());
			return rtn;
		}
	}	
	public String httpRequesttext(String requestUrl, String requestMethod, String outputStr,String lj) 
	{
		String rtn="";
		try{
			StringBuffer buffer = new StringBuffer();
			URL url = new URL(requestUrl);
			HttpsURLConnection httpUrlConn = (HttpsURLConnection) url.openConnection();

			httpUrlConn.setDoOutput(true);
			httpUrlConn.setDoInput(true);
			httpUrlConn.setUseCaches(false);

			httpUrlConn.setRequestMethod(requestMethod);

			if ("GET".equalsIgnoreCase(requestMethod)) {
				httpUrlConn.connect();
			}

			if (outputStr != null) {
				OutputStream outputStream = httpUrlConn.getOutputStream();
				outputStream.write(outputStr.getBytes("UTF-8"));
				outputStream.close();
			}

			BufferedInputStream bis = new BufferedInputStream(httpUrlConn.getInputStream());
	        FileOutputStream fos = new FileOutputStream(new File(lj));
	        byte[] buf = new byte[8096];
	        int size = 0;
	        while ((size = bis.read(buf)) != -1){
	          fos.write(buf, 0, size);
	        }
	        fos.close();
	        bis.close();

	        httpUrlConn.disconnect();
			return rtn;
		} 
		catch (Exception e)
		{
			ToolBean.info("httpRequest发生错误："+e.toString());
			return rtn;
		}
	}
}
